package com.beaven.imclient.fragment;

import android.app.ProgressDialog;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.beaven.imclient.R;
import com.beaven.imclient.base.BaseFragment;
import com.beaven.imclient.presenter.account.UpdateUserContract;
import com.beaven.imclient.presenter.account.UpdateUserPresenter;
import com.beaven.imclient.utils.ToastUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class NickFragment extends BaseFragment implements UpdateUserContract.View {

  public static NickFragment newInstance() {
    return new NickFragment();
  }

  @Override protected int getLayoutId() {
    return R.layout.fragment_nick;
  }

  private EditText edit_nick_update;
  private UpdateUserContract.Presenter presenter;
  private ProgressDialog dialog;

  @Override protected void initView(View rootView) {
    super.initView(rootView);
    edit_nick_update = (EditText) rootView.findViewById(R.id.edit_nick_update);
    Button bt_save = (Button) rootView.findViewById(R.id.bt_save);
    bt_save.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        presenter.updateUser();
      }
    });
  }

  @Override protected void initData() {
    super.initData();
    setPresenter(new UpdateUserPresenter(this));
  }

  @Override public void onResume() {
    super.onResume();
    presenter.subscribe();
  }

  @Override public void onPause() {
    super.onPause();
    presenter.unSubscribe();
  }

  @Override public void setPresenter(UpdateUserContract.Presenter presenter) {
    this.presenter = presenter;
  }

  @Override public String getNick() {
    return edit_nick_update.getText().toString().trim();
  }

  @Override public void showLoading() {
    dialog = ProgressDialog.show(getActivity(), "", getString(R.string.load_now));
  }

  @Override public void hideLoading() {
    if (dialog != null) {
      dialog.dismiss();
    }
  }

  @Override public void showText(int stringId) {
    ToastUtil.show(stringId);
  }

  @Override public void finish() {
    getActivity().finish();
  }
}
